package springboot_cae_thymeleaf.controller;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import springboot_cae_thymeleaf.entity.CaeEntity;
import springboot_cae_thymeleaf.service.CaeService;

@Controller
public class CaeController {

	@Autowired
	private CaeService caeService;
	
	//跳转到list页面，展示所有数据
	@RequestMapping(value = "/list")
	public String list(Model model) {
		List<CaeEntity> caeEntities=caeService.selectService();
		//model是作为前后台的一个交互作用的,往前台传数据，可以传对象，可以传List，通过el表达式 ${}可以获取到，类似于request.setAttribute("sts",sts)效果一样
        model.addAttribute("caes", caeEntities);
        return "list";
	}
	
	//跳转到edit页面，并将根据id查找到部门数据传入edit
	@RequestMapping("/toEdit")
	 public String toEdit(Model model,int id) {
        CaeEntity caeEntity = caeService.selectByService(id);
        model.addAttribute("cae", caeEntity);
        return "edit";
    }

	//编辑部门信息，更新之后再转到list页面
	@RequestMapping("/edit")
    public String edit(CaeEntity caeEntity) {
        caeService.updateService(caeEntity);
        return "redirect:/list";
    }
	
	//删除部门信息
	@RequestMapping("/toDelete")
    public String delete(int id) {
        caeService.deleteService(id);
        return "redirect:/list";
    }
	
	//跳转到add页面
	@RequestMapping("/toAdd")
    public String toAdd() {
        return "add";
    }
	
	//添加部门信息，添加完成之后转到list页面
	@RequestMapping("/add")
    public String add(CaeEntity caeEntity) {
        caeService.addService(caeEntity);
        return "redirect:/list";
    }
}